package com.xxl.job.admin.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@Component//尽量加上这个
@WebListener//声明为监听器
@Profile("local")//进本地模式使用ssh隧道连接
@Slf4j
public class SshListener implements ServletContextListener {

    @Autowired
    private SshConfiguration sshConnectionConfig;

    public SshListener() {
        super();
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("SSH连接 initialized ... !");
        new Thread(() -> {//异步调用
            try {
                sshConnectionConfig.createSSH();
            } catch (Throwable e) {

                e.printStackTrace(); // 连接失败
            }
        }).start();

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("SSH连接 destroyed ... !");
        sshConnectionConfig.closeSSHMysql();//断开ssh连接
    }
}